﻿using SuperSocialShare.Error;
using SuperSocialShare.Authorize;
using System;

namespace SuperSocialShare.Clients
{
    public abstract class SocialClient
    {
        public delegate void GetAuthorizeInfoCompletedEventHandler(object sender, AsyncEventArgs e);
        public event GetAuthorizeInfoCompletedEventHandler GetAuthorizeInfoCompleted;

        public delegate void GetUserInfoCompletedEventHandler(object sender, AsyncEventArgs e);
        public event GetUserInfoCompletedEventHandler GetUserInfoCompleted;

        public delegate void ShareCompletedEventHandler(object sender, AsyncEventArgs e);
        public event ShareCompletedEventHandler ShareCompleted;

        public delegate void RevokeAuthorizeCompletedEventHandler(RevokeAuthorizeResult result);
        public event RevokeAuthorizeCompletedEventHandler RevokeAuthorizeCompleted;

        /// <summary>
        /// 获取或设置第三方应用的App Key
        /// </summary>
        public String ClientId { set; get; }

        /// <summary>
        /// 获取或设置第三方应用的App Secret
        /// </summary>
        public String ClientSecret { set; get; }

        /// <summary>
        /// 获取或设置第三方应用的回调地址
        /// </summary>
        public String CallbackUrl { set; get; }

        /// <summary>
        /// 获取请求授权地址
        /// </summary>
        protected abstract string AuthorizeUrl { get; }

        /// <summary>
        /// 获取取得access_token的地址
        /// </summary>
        protected abstract string GetAccessTokenUrl { get; }

        /// <summary>
        /// 获取用户信息的url
        /// </summary>
        protected abstract string GetUserInfoUrl { get; }

        /// <summary>
        /// 获取分享状态的url
        /// </summary>
        protected abstract string UpdateStatusUrl { get; }

        /// <summary>
        /// 获取上传图片的url
        /// </summary>
        protected abstract string UploadImageUrl { get; }

        /// <summary>
        /// 获取取消授权的url
        /// </summary>
        protected abstract string RevokeAuthorizeUrl { get; }

        /// <summary>
        /// 获取社区类型
        /// </summary>
        public abstract SocialType SocialType { get; }

        /// <summary>
        /// 请求授权
        /// </summary>
        internal abstract string GetRequestAuthorizeUrl();

        /// <summary>
        /// 处理请求授权后的返回数据
        /// </summary>
        /// <param name="data">返回数据</param>
        internal abstract void ProcessResponseDataOfAuthorize(string data);

        /// <summary>
        /// 分享文本
        /// </summary>
        public abstract void ShareText(string text);

        /// <summary>
        /// 分享文本，并上传一张图片
        /// </summary>
        /// <param name="text">要分享的文本</param>
        /// <param name="imgdata">上传图片的二进制数据</param>
        public abstract void ShareTextWithImage(string text, byte[] imgdata);

        /// <summary>
        /// 取消授权
        /// </summary>
        public abstract void RevokeAuthorizeAysnc();

        protected virtual void GetAccessTokenAsync(string code) { }

        /// <summary>
        /// 获取获得授权的用户名
        /// </summary>
        /// <param name="userid">授权信息</param>
        protected abstract void GetUserInfoAsync(AuthorizeInfo info);

        
        protected virtual void OnGetAuthorizeInfoCompleted(AsyncEventArgs e)
        {
            if (GetAuthorizeInfoCompleted != null)
                GetAuthorizeInfoCompleted(this, e);
        }

        protected virtual void OnGetUserInfoCompleted(AsyncEventArgs e)
        {
            if (GetUserInfoCompleted != null)
                GetUserInfoCompleted(this, e);
        }

        protected virtual void OnShareCompleted(AsyncEventArgs e)
        {
            if (ShareCompleted != null)
                ShareCompleted(this, e);
        }

        protected virtual void OnRevokeAuthorizeCompleted(RevokeAuthorizeResult result)
        {
            if (RevokeAuthorizeCompleted != null)
                RevokeAuthorizeCompleted(result);
        }
    }
}
